package com.ossjk.api.controller;


import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonMethod;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.oa.attendance.entity.Atdreport;
import com.ossjk.oa.attendance.entity.Vemp2Dept;
import com.ossjk.oa.attendance.service.IAtdreportService;
import com.ossjk.oa.attendance.service.impl.AtdreportServiceImpl;
import com.ossjk.oa.attendance.service.impl.AtdtableServiceImpl;
import com.ossjk.oa.finance.service.IReimburseService;
import com.ossjk.oa.system.entity.Department;
import com.ossjk.oa.system.entity.Employee;
import com.ossjk.oa.system.service.IDepartmentService;
import com.ossjk.oa.system.service.IEmployeeService;
import com.ossjk.oa.system.service.IPostService;
import com.ossjk.oa.system.vo.EmployeeVo;
import com.ossjk.oa.v_emp2dept.entity.V_emp2dept;
import com.ossjk.oa.v_emp2dept.service.V_emp2deptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.util.*;


@Api(description = "请假接口")
@RestController
@RequestMapping("/api/leave")
public class LeaveApiController extends BaseController {
    @Autowired
    private IReimburseService iReimburseService;
    @Autowired
    private V_emp2deptService v_emp2deptService;
    @Autowired
    private IAtdreportService iAtdreportService;
    @Autowired
    private IDepartmentService iDepartmentService;
    @Autowired
    private IEmployeeService iEmployeeService;
    @Autowired
    private IPostService iPostService;

    /*
     * 个人可以查询自己的月度请假信息
     * author:林镇彬
     *
     * */
    @ApiOperation(value = "请假列表", notes = "请假列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "month", value = "月份", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "eid", value = "用户", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "did", value = "部门id", paramType = "query", dataType = "String"),

    })
    @GetMapping("getLeaveApplication")
    public ResponseBean<List<Atdreport>> getLeaveApplication(String month, String eid,String did) {


//        1、判断前端时候有传递，部门id，员工id，月份,如果都没有可以通过token获取用户id
        if (CommonUtil.isBlank(month)) {
            Calendar now = Calendar.getInstance();
            month = CommonMethod.calMth(0, now.getTime()); // 默认本月
        }
        if (CommonUtil.isBlank(eid)) {
            //默认自己的
            eid = getCurrnetUserId();
        }


//        2、通过视图v_emp2dept获取用户的部门和level
        Wrapper wrapper = Condition.create().isWhere(true).eq("id", eid);

        Vemp2Dept vemp2Dept = iAtdreportService.selectDaoHao(wrapper);
        List list = new ArrayList();
        list.add(2);
        list.add(3);
        list.add(4);

        Wrapper wrapper2 = Condition.create().in("type",list);
        // 类型：1-加班、2-带薪年假、3-带薪调休、4-普通请假，5-外出
//        3、判断level：=2:显示部门所有信息，>2: 显示个人，=1：总经理：功能中要有一个部门id的参数查询
        if (vemp2Dept.getLevel()>2) {
            //>2: 显示个人
            wrapper2.eq("eid", eid).like("date", month);
        } else if (vemp2Dept.getLevel()==2) {
            //=2:显示自己部门所有信息
            wrapper2.like("date", month).eq("did",vemp2Dept.getDid());
        } else {
            wrapper2.like("date", month);
        }

        return (ResponseBean<List<Atdreport>>) responseSuccess(iAtdreportService.selectList(wrapper2));
    }

    @ApiOperation(value = "查询所有部门信息", notes = "查询所有部门信息")
    @GetMapping("SelectAllDept")
    public ResponseBean<List<V_emp2dept>> queryAllDept(){
        Wrapper wrapper1=Condition.create();

        return (ResponseBean<List<V_emp2dept>>) responseSuccess(iDepartmentService.selectAllDept(wrapper1)) ;
    }

    @ApiOperation(value = "按照传入的部门id查询其部门下的所有人信息", notes = "按照传入的部门id查询其部门下的所有人信息")
    @ApiImplicitParam(name = "did", value = "部门id", paramType = "query", dataType = "String", required = true)
//    查询每个部门人数接口
    @GetMapping("SelectDeptPer")
    public ResponseBean<List<V_emp2dept>> queryDept1(String did){
        Wrapper wrapper1=Condition.create().isWhere(true).eq("did",did);
        return (ResponseBean<List<V_emp2dept>>) responseSuccess(v_emp2deptService.selectByPer(wrapper1)) ;
    }











    /**
     * 4.2.1 请假新增
     *
     * author:林镇彬
     */


    @ApiOperation(value = "请假新增", notes = "请假新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "请假日期", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "duration", value = "请假时长", paramType = "query", dataType = "String", required = true),

            @ApiImplicitParam(name = "remarks", value = "请假备注", paramType = "query", dataType = "String")
    })
    @PostMapping("insert")
    public ResponseBean<Atdreport> insert(@ApiIgnore Atdreport atdreport) {

//        /**
//         * 员工id,并不是必填
//         */
        if (CommonUtil.isBlank(atdreport.getEid())) {
            atdreport.setEid(getCurrnetUserId());
        }
//        /**
//         * 部门id,并不是必填
//         */
        if (CommonUtil.isBlank(atdreport.getDid())) {
            // 查询该员工的部门
            Department department = iDepartmentService.selectByEid(getCurrnetUserId());
            // 设置部门id
            atdreport.setDid(department.getId());
        }
//        /**
//         * 日期，前端传的，是否必填：是
//         */
        if (CommonUtil.isBlank(atdreport.getDate())) {
            return (ResponseBean<Atdreport>) responseFail("date：请假时间不能为空");
        }
        // 格式化Date 如: yyyyMMdd
        String dateStr = CommonUtil.date0(atdreport.getDate());
        // 年
        atdreport.setYear(dateStr.substring(0, 4));
        // 月
        atdreport.setMonth(dateStr.substring(4, 6));
        // 日
        atdreport.setDay(dateStr.substring(6, 8));
//        /**
//         * 类型：1-加班、2-带薪年假、3-带薪调休、4-普通请假，5-外出,并不是必填
//         */
        if (CommonUtil.isBlank(atdreport.getType())) {
            atdreport.setType(new BigDecimal(4));
        }
//        /**
//         * 备注:是否必填，不是
//         */


//        /**
//         * 审核状态：1-待审核、2-通过、3-不通过，后台定义的,默认值
//         */
        atdreport.setRwst(new BigDecimal(1));

//        /**
//         * 时长，前端传的，是否必填：是
//         */

        if (CommonUtil.isBlank(atdreport.getDuration())) {
            return (ResponseBean<Atdreport>) responseFail("duration：请假时长不能为空");
        }


        if (iAtdreportService.insert(atdreport)) {
            return (ResponseBean<Atdreport>) responseSuccess();
        } else {
            return (ResponseBean<Atdreport>) responseFail();
        }
    }


    /**
     * 获取请假信息
     *
     * @author:林镇彬
     */
    @ApiOperation(value = "获取请假信息", notes = "获取请假信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "eid", value = "eid", paramType = "query", dataType = "String", required = true)
    })
    @GetMapping("getAtdreportById")
    public ResponseBean<Atdreport> getAtdreportById(String eid) {
        if (CommonUtil.isBlank(eid)) {
            return (ResponseBean<Atdreport>) responseFail("eid:不能为空");
        }
        return (ResponseBean<Atdreport>) responseSuccess(iAtdreportService.selectById(eid));
    }


    /**
     * 修改请假信息
     *
     * @author:林国标
     */
    @ApiOperation(value = "请假修改", notes = "请假修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "date", value = "请假日期", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "duration", value = "请假时长", paramType = "query", dataType = "String", required = true),

            @ApiImplicitParam(name = "remarks", value = "请假备注", paramType = "query", dataType = "String",required = true)
    })
    @PutMapping("update")
    public ResponseBean update(@ApiIgnore Atdreport atdreport) {
        //id必填
        if (CommonUtil.isBlank(atdreport.getId())) {
            return (ResponseBean) responseFail("id:不能为空");
        }
        //判断审核状态是否=1
        Atdreport atdreport1 = iAtdreportService.selectById(atdreport.getId());
        if (!CommonUtil.isEquals(atdreport1.getRwst(), 1)) {
            return (ResponseBean) responseFail("rwst:状态不是待审核，不能修改");
        }
//        /**
//         * 员工id,并不是必填
//         */
        if (CommonUtil.isBlank(atdreport.getEid())) {
            atdreport.setEid(getCurrnetUserId());
        }
//        /**
//         * 部门id,并不是必填
//         */
        if (CommonUtil.isBlank(atdreport.getDid())) {
            // 查询该员工的部门
            Department department = iDepartmentService.selectByEid(getCurrnetUserId());
            // 设置部门id
            atdreport.setDid(department.getId());
        }
//        /**
//         * 日期，前端传的，是否必填：是
//         */
        if (CommonUtil.isBlank(atdreport.getDate())) {
            return (ResponseBean) responseFail("date：请假时间不能为空");
        }
        // 格式化Date 如: yyyyMMdd
        String dateStr = CommonUtil.date0(atdreport.getDate());
        // 年
        atdreport.setYear(dateStr.substring(0, 4));
        // 月
        atdreport.setMonth(dateStr.substring(4, 6));
        // 日
        atdreport.setDay(dateStr.substring(6, 8));
//        /**
//         * 类型：1-加班、2-带薪年假、3-带薪调休、4-普通请假，5-外出,并不是必填
//         */
        if (CommonUtil.isBlank(atdreport.getType())) {

            atdreport.setType(new BigDecimal(4));
        }
//        /**
//         * 备注:是否必填，不是
//         */


//        /**
//         * 审核状态：1-待审核、2-通过、3-不通过，后台定义的,默认值
//         */
        atdreport.setRwst(new BigDecimal(1));

//        /**
//         * 时长，前端传的，是否必填：是
//         */

        if (CommonUtil.isBlank(atdreport.getDuration())) {
            return (ResponseBean) responseFail("duration：请假时长不能为空");
        }


        if (iAtdreportService.updateById((atdreport))) {
            return (ResponseBean) responseSuccess();
        } else {
            return (ResponseBean) responseFail();
        }
    }


    /**
     * 删除请假信息
     *
     * @author:林国标
     */


    @ApiOperation(value = "请假删除", notes = "请假删除")
    @ApiImplicitParam(name = "id", value = "数据id", paramType = "query", dataType = "String", required = true)
    @DeleteMapping("delete")
    public ResponseBean delete(String id) {
        if (CommonUtil.isBlank(id)) {
            return (ResponseBean) responseFail("id：不能为空");
        }
        Wrapper wrapper = Condition.create().eq("id", id).eq("rwst", 1);
        if (iAtdreportService.delete(wrapper)) {
            return (ResponseBean) responseSuccess();
        } else {
            return (ResponseBean) responseFail();
        }
    }


    /**
     * 对未处理的可以审批请假申请
     *
     * @author:林镇彬
     */


    @ApiOperation(value = "审批请假申请", notes = "审批请假申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数据id", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "rwst", value = "审核是否通过，只能是2-通过、3-不通过", paramType = "query", dataType = "BigDecimal", required = true),
            @ApiImplicitParam(name = "rwrs", value = "审核原因", paramType = "query", dataType = "String", required = true)
    })
    @PutMapping("CheckRwst")
    public ResponseBean CheckRwst(String id, BigDecimal rwst, String rwrs) {

        Atdreport atdreport = iAtdreportService.selectById(id);
        atdreport.setRwst(rwst);
        atdreport.setRwrs(rwrs);
       return (ResponseBean) responseSuccess(iAtdreportService.updateById(atdreport));
    }

    /**
     * 月度导航信息
     *
     * @author:林国标
     */

    //   4.4 按全公司人员的请假信息做月度导航 ([上一月][本月][下一月])
    @GetMapping("/MonthNavigation")

    @ApiOperation(value = "月度导航", notes = "传入mth属性--例如：mth=2020-07")
    @ApiImplicitParam(name = "mth", value = "数据id", paramType = "query", dataType = "String")
    public Object MonthNavigation(String mth) {

        //查出所有请假的员工
        //type类型：1-加班、2-带薪年假、3-带薪调休、4-普通请假，5-外出
//        Wrapper wrapper = Condition.create().isWhere(true).eq("type", 4);
//        Calendar now = Calendar.getInstance();
        if (mth == null) {
            Calendar now = Calendar.getInstance();
            if (now.get(Calendar.DAY_OF_MONTH) > 20) {
                mth = CommonMethod.calMth(0, now.getTime()); // 默认本月
            } else {
                mth = CommonMethod.calMth(-1, now.getTime()); // 默认上月
            }
        }
//        ArrayList list = new ArrayList();
//
//        String   mth = CommonMethod.calMth(0, now.getTime()); // 默认本月
//        String    mth1 = CommonMethod.calMth(-1, now.getTime()); // 默认本月
//        String   mth2 = CommonMethod.calMth(+1, now.getTime());
//        list.add(mth);
//        list.add(mth1);
//        list.add(mth);
//        System.out.println(mth+"---"+mth1);

        return responseSuccess(iAtdreportService.selectPreNext(mth));
    }


}
